package com.controller;

import com.github.pagehelper.PageInfo;
import com.pojo.Role;
import com.pojo.Type;
import com.service.TypeService;
import com.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class TypeController {
   @Autowired
   private TypeService typeService;

    /**
     * 根据id查询项目分类信息
     * @param id
     * @return
     */
    @RequestMapping("/selectTypeById")
    public Object selectTypeById(long id){
        R r=new R<Role>();
        Type type = typeService.selectTypeById(id);
        if(type!=null){
            r.setCode(200);
            r.setData(type);
        }else{
            r.setCode(400);
            r.setMessage("该项目分类不存在");
        }
        return r;
    }

    /**
     *name模糊查询 分页查询
     * @param pageNum
     * @param name
     * @return
     */
    @RequestMapping("/selectTypeByName")
    public Object selectTypeByName(@RequestParam(value = "pageNum",required = true,defaultValue= "1") int pageNum, @RequestParam(value = "name",required = true,defaultValue= "") String name  ){
        R r=new R<Role>();
        Map map=new HashMap();
        map.put("pageNum",pageNum);
        map.put("name",name);
        PageInfo pageInfo = typeService.selectTypeByName(map);
        r.setCode(200);
        r.setData(pageInfo);
        return r;
    }

    /**
     * 更新项目分类信息
     * @param type
     * @return
     */
    @RequestMapping("/updateType")
    public Object updateType(Type type){
        R r=new R<Role>();
        int i = typeService.updateType(type);
        if(i!=0){
            r.setCode(200);
            r.setMessage("更新成功");
            r.setData(1);
        }else{
            r.setCode(400);
            r.setMessage("更新失败");
            r.setData(0);
        }
        return r;
    }


    /**
     * 插入项目分类
     * @param type
     * @return
     */
    @RequestMapping("/insertType")
    public Object insertType(Type type){
        R r=new R<Role>();
        int i = typeService.insertType(type);
        if(i!=0){
            r.setCode(200);
            r.setMessage("插入成功");
            r.setData(1);
        }else{
            r.setCode(400);
            r.setMessage("插入失败");
            r.setData(0);
        }
        return r;
    }

    /**
     * 根据id删除项目分类
     * @param id
     * @return
     */
    @RequestMapping("/deleteTypeById")
    public Object deleteTypeById(long id){
        R r=new R<Role>();
        int i = typeService.deleteTypeById(id);
        if(i!=0){
            r.setCode(200);
            r.setMessage("删除成功");
            r.setData(1);
        }else{
            r.setCode(400);
            r.setMessage("删除失败");
            r.setData(0);
        }
        return r;
    }

    @RequestMapping("/selectAlltype")
    public Object selectAllType(){
        List<Type> types = typeService.selectAllType();
        return types;
    }
}
